package com.rg.servlet;

import com.rg.model.UserModel;
import com.rg.service.UserLoginService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/UserLoginServlet")
public class UserLoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //接收用户登录时的账号密码
        String userName = req.getParameter("loginName");
        String userPwd = req.getParameter("password");
        UserLoginService userLoginService = new UserLoginService();
        UserModel user = userLoginService.checkUser(userName,userPwd);
        if(user==null){
            req.setAttribute("loginError","<label style='color:red'>账号或密码错误，请重新登录！</label>");
            req.getRequestDispatcher("login.jsp").forward(req,resp);
        }else {
            HttpSession session = req.getSession();
            session.setAttribute("user",user);
            resp.sendRedirect("GoodsTypeServlet");
        }
    }
}
